package com.hamster.crmpre.web;

import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.mapper.MessageAllianceMapper;
import com.hamster.crmpre.mapper.MessageBuyVerifyMapper;
import com.hamster.crmpre.mapper.MessageChangeCourseMapper;
import com.hamster.crmpre.mapper.PersonalMessageMapper;
import com.hamster.crmpre.pojo.*;
import com.hamster.crmpre.pojo.dto.SaveLeveragePlanDTO;
import com.hamster.crmpre.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Lizhipeng
 * @date ：Created in 2019/12/27 11:09
 * @description：消息列表查看按钮的控制处理【不同类型对应不同的接口】
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("messageView")
public class MessageViewController {
    @Autowired
    private PhoneUserService phoneUserService;
    @Autowired
    private MessageViewService messageViewService;
    @Autowired
    private MessageQueryService messageQueryService;
    @Autowired
    private ProjectBuyService projectBuyService;
    @Autowired
    private CustomerAssetsService customerAssetsService;
    @Autowired
    private HandOverService handOverService;
    @Autowired
    private CustomerAssistantService customerAssistantService;
    @Autowired
    private MessageChangeCourseMapper messageChangeCourseMapper;
    @Autowired
    private FranchiseProjectsService franchiseProjectsService;
    @Autowired
    private MessageAllianceMapper messageAllianceMapper;
    @Autowired
    private LeveragePlanService leveragePlanService;
    @Autowired
    private MessageBuyVerifyMapper messageBuyVerifyMapper;

    /**
     * 获取HandOverAssistant对象信息的请求处理
     *
     * @return HandOverAssistant对象信息
     */
    @GetMapping("getHandOverAssistant")
    public ResponseEntity<HandOverAssistant> getHandOverAssistant(@RequestParam(value = "id") String id) {
        //messageQueryService.setRead(id);
        PersonalMessage personalMessage = messageQueryService.get(id);
        String messageLink = personalMessage.getMessageLink();
        HandOverAssistant handOverAssistant = messageViewService.getHandOverAssistant(messageLink);
        return ResponseEntity.ok(handOverAssistant);
    }

    /**
     * 转交助教点击接收按钮
     * @param map
     */
    @PostMapping("getHandOverAssistant/receive")
    public void receive(@RequestBody Map<String, String> map) {
        customerAssistantService.receive(map);
    }

    /**
     * 根据消息id获取开发客户审批
     *
     * @param id 个人信息中心id
     * @return 开发客户审批界面所需内容
     */
    @GetMapping("getDevelopCustomerApproval")
    public Map<String, Object> getDevelopCustomerApproval(@RequestParam(value = "id") String id) {
        //messageQueryService.setRead(id);
        Map<String, Object> map = new HashMap<>();
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(id);
        //获取要操作的对象的详细消息的对象
        String messageLink = personalMessage.getMessageLink();
        MessageBuyVerify messageBuyVerify = messageViewService.getMessageBuyVerify(messageLink);
        //填充数据
        map.put("id", id);//个人信息中心id
        map.put("messageLink", messageLink);//messageLink用来查到messageBuyVerify中的数据
        map.put("sender", personalMessage.getSenderName());//发送人
        map.put("messageType", personalMessage.getMessageType());//任务类型
        map.put("content", messageBuyVerify.getContent());//任务内容
        map.put("profit", messageBuyVerify.getMoney());//提成金额
        map.put("state", messageBuyVerify.getState());//当前消息详情的状态    0未读   1同意   2拒绝
        return map;
    }

    /**
     * 开发客户审批通过按钮功能
     *
     * @param map 从前台拿到的页面数据
     */
    @PostMapping("getDevelopCustomerApproval/adopt")
    @Transactional
    public void developCustomerApprovalAdopt(@RequestBody Map<String, Object> map) {
        messageQueryService.setRead((String) map.get("id"));//标记消息已读
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get((String) map.get("id"));
        //获取要操作的对象的详细消息的对象
        String messageLink = personalMessage.getMessageLink();


        MessageBuyVerify messageBuyVerify = messageViewService.getMessageBuyVerify(messageLink);
        messageBuyVerify.setMoney(Double.parseDouble(map.get("提成").toString()));
        if (map.get("select").equals("1")) {//1为选择同意按钮
            messageBuyVerify.setState("1");
            messageBuyVerify.setAuditorId(LoginInterceptor.getLoginUser().getUserId());

            //根据手机号，查询该登录用户的姓名
            String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
            //messageBuyVerify.setAuditorName(LoginInterceptor.getLoginUser().getName());
            messageBuyVerify.setAuditorName(userName);
        } else {//2为选择拒绝按钮
            messageBuyVerify.setState("2");
        }
        messageBuyVerifyMapper.updateById(messageBuyVerify);
        projectBuyService.buyProjectPassed((String) map.get("id"));
    }

    /**
     * 根据消息id 获取转交客户资产审批
     *
     * @param id 个人信息中心id
     * @return 开发客户审批界面所需内容
     */
    @GetMapping("getHandOverCustomerAssetsApproval")
    public Map<String, Object> getHandOverCustomerAssetsApproval(@RequestParam(value = "id") String id) {
        // messageQueryService.setRead(id);
        Map<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(id);
        //获取要操作的对象的详细消息的对象的id
        String messageLink = personalMessage.getMessageLink();
        HandOverCustomerAssets handOverCustomerAssets = messageViewService.getHandOverCustomerAssets(messageLink);
        System.out.println(handOverCustomerAssets);
        //填充数据
        map.put("id", id);//个人信息中心id
        map.put("sender", personalMessage.getSenderName());//发送者
        map.put("messageType", personalMessage.getMessageType());//任务类型
        /**
         * 任务内容获取
         */
        map.put("customerId",handOverCustomerAssets.getCustomerId());//原始人的id
        map.put("customerName",handOverCustomerAssets.getCustomerName());//原始客户的姓名
        map.put("content",handOverCustomerAssets.getContent());//任务内容
        map.put("transferCustomerName",handOverCustomerAssets.getTransferCustomerName());//目标客户的姓名
        map.put("transferCustomerId",handOverCustomerAssets.getTransferCustomerId());//目标客户的id
        map.put("state",handOverCustomerAssets.getState());
        return map;
    }

    /**
     * 转交客户资产通过按钮功能
     * @param map    包括select和id select  1  通过，2  不通过   id个人消息中心消息id
     */
    @PostMapping("getHandOverCustomerAssetsApproval/adopt")
    @Transactional
    public void handOverCustomerAssetsApprovalAdopt(@RequestBody Map<String, Object> map) {
        messageQueryService.setRead((String) map.get("id"));//标记消息已读
        if ("1".equals(map.get("select"))){//通过,select  1  通过，2  不通过
            customerAssetsService.handOverPassed((String) map.get("id"));
        }else {//不通过
            customerAssetsService.handOverRefuse((String) map.get("id"));
        }


    }


    /**
     * 查看问卷详情
     *
     * @param id 个人信息中心的id
     * @return json 键值对，问卷详情
     */
    @GetMapping("getMessageQuestionnaire")
    public Map<String, String> getMessageQuestionnaire(@RequestParam(value = "id") String id) {
        Map<String, String> map = new HashMap<>();
        //查询个人中心消息
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(id);
        //修改成已读
        //messageQueryService.setRead(id);

        //获取要操作的对象的详细消息的对象的id
        String messageLink = personalMessage.getMessageLink();
        //查询问卷内容
        MessageQuestionnaire messageQuestionnaire = messageQueryService.getMessageQuestionnaire(messageLink);

        map.put("personalMessageId",id);            //消息中心的id
        map.put("sender", personalMessage.getSenderName());//任务发起者
        map.put("messageType", personalMessage.getMessageType());//任务类型
        map.put("content", messageQuestionnaire.getContent());//问卷内容
        map.put("questionnaireState", messageQuestionnaire.getState());//当前问卷的状态
        map.put("questionnaireId", messageQuestionnaire.getId());//当前问卷的id
        return map;
    }

    /**
     * 提交问卷
     *
     * @param messageQuestionnaire 问卷对象
     */
    @PostMapping("getMessageQuestionnaire/submit")
    public void messageQuestionnaireSubmit(@RequestParam String personalMessageId,@RequestBody MessageQuestionnaire messageQuestionnaire) {
        messageQueryService.messageQuestionnaireSubmit(personalMessageId,messageQuestionnaire);
    }

    /**
     * 员工查询，客户返回的问卷结果
     *
     * @return
     */
    @GetMapping("getMessageQuestionnaire/result")
    public ResponseEntity<List<MessageQuestionnaire>> listMessageQuestionnaireResults() {
        String userId = LoginInterceptor.getLoginUser().getUserId();//员工  的 id
        List<MessageQuestionnaire> list = messageQueryService.listMessageQuestionnaireResults(userId);

        return ResponseEntity.ok(list);
    }

    /**
     * 员工查看自己用户，提交的问卷的详情
     *
     * @param id 问卷详情的id
     * @return
     */
    @GetMapping("getMessageQuestionnaire/detail")
    public ResponseEntity<MessageQuestionnaire> getMessageQuestionnaireDetail(@RequestParam String id) {
        MessageQuestionnaire messageQuestionnaire = messageQueryService.getMessageQuestionnaireDetail(id);
        return ResponseEntity.ok(messageQuestionnaire);
    }

    /**
     * 转交个人开发客户提交
     *  领导审核，移交客户
     * @param map
     */
    @PostMapping("handOverDevelopCustomer/submit")
    public void handOverDevelopCustomerSubmit(@RequestBody Map<String, String> map) {
//        handOverService.developCustomerSubmit(map);
        handOverService.divertCustomerSubmit(map);
    }





    /**
     * 获取转交个人开发客户页面的内容
     *  领导获取 移交客户 页面的内容
     * @param id
     */
    @GetMapping("handOverDevelopCustomer/get")
    public ResponseEntity<HandOverPersonalDevelopmentCustomer> handOverDevelopCustomerGet(@RequestParam String id) {
//        Map<String, String> map = new HashMap<>();
        //查询个人中心消息
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(id);
        //修改成已读
        //messageQueryService.setRead(id);

        //获取要操作的对象的详细消息的对象的id
        String messageLink = personalMessage.getMessageLink();
        //获取详细消息的内容
        HandOverPersonalDevelopmentCustomer handOverPersonalDevelopmentCustomer = handOverService.handOverDevelopCustomerGet(messageLink);
//        map.put("senderName", personalMessage.getSenderName());
//        map.put("senderId", personalMessage.getSenderId());
//        map.put("receiverName", personalMessage.getReceiverName());
//        map.put("receiverId", personalMessage.getReceiverId());
//        map.put("id", handOverPersonalDevelopmentCustomer.getId());
//        map.put("employeeId", handOverPersonalDevelopmentCustomer.getEmployeeId());
//        map.put("employeeName", handOverPersonalDevelopmentCustomer.getEmployeeName());
//        map.put("state", handOverPersonalDevelopmentCustomer.getState());
        return ResponseEntity.ok(handOverPersonalDevelopmentCustomer);

    }

    /**
     * 转交 不同部门的客户负责人 提交
     *
     * @param map
     */
    @PostMapping("handOverCustomer/submit")
    public void handOverVipSubmit(@RequestBody Map<String, String> map) {
        handOverService.vip(map);
    }

    /**
     * 获取 转交不同部门 客户的详细内容
     *
     * @param id
     * @return
     */
    @GetMapping("handOverCustomer/get")
    public Map<String, String> handOverVipGet(@RequestParam String id) {
        Map<String, String> map = new HashMap<>();
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(id);
        //修改成已读
        //messageQueryService.setRead(id);

        //获取要操作的对象的详细消息的对象的id
        String messageLink = personalMessage.getMessageLink();
        HandOverVip vip = handOverService.getVip(messageLink);

        map.put("receiverId", personalMessage.getReceiverId());
        map.put("receiverName", personalMessage.getReceiverName());
        map.put("senderId", personalMessage.getSenderId());
        map.put("senderName", personalMessage.getSenderName());
        map.put("messageType", personalMessage.getMessageType());
        map.put("employeeId", vip.getEmployeeId());
        map.put("employeeName", vip.getEmployeeName());
        map.put("id", vip.getId());
        map.put("state", vip.getState());
        map.put("customerId", vip.getVipCustomerId());//客户的id
        map.put("customerName", vip.getVipCustomerName());//客户的姓名
        return map;
    }


    /**
     * 获取更改课程的通知内容
     *
     * @param id 消息中心的id
     * @return
     */
    @GetMapping("getChangeCourseNotify")
    public Map<String, String> getChangeCourseNotify(@RequestParam String id) {
        Map<String, String> map = new HashMap<>();
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(id);
        //修改成已读
        messageQueryService.setRead(id);

        //获取要操作的对象的详细消息的对象的id
        String messageLink = personalMessage.getMessageLink();
        MessageChangeCourse messageChangeCourse = messageChangeCourseMapper.selectById(messageLink);

        map.put("sender", personalMessage.getSenderName());//任务发起者
        map.put("messageType", personalMessage.getMessageType());//任务类型
        map.put("content", messageChangeCourse.getContent());//内容
        map.put("customerName", messageChangeCourse.getCustomerName());//客户姓名
        map.put("customerPhone", messageChangeCourse.getCustomerPhone());//客户手机号
        map.put("customerId", messageChangeCourse.getCustomerId());//客户id
        map.put("remark", messageChangeCourse.getRemark());//备注  差价
        return map;
    }

    /**
     * 获取项目加盟消息详情页面数据
     *
     * @param id 个人消息中心的该条消息的messageLink
     * @return
     */
    @GetMapping("getMessageAlliance")
    public MessageAlliance getMessageAlliance(@RequestParam String id) {
        //messageQueryService.setRead(id);
        PersonalMessage personalMessage = messageQueryService.get(id);
        String messageLink = personalMessage.getMessageLink();
        MessageAlliance messageAlliance = messageViewService.getMessageAlliance(messageLink);
        return messageAlliance;
    }

    /**
     * 项目加盟处理操作

//     * @param select 1 通过   2 不通过
//     * @param id        消息中心的id
     */
//    @PostMapping("alliancePass")
//    public void AlliancePass(@RequestBody EmployeePerformance employeePerformance, @RequestParam String select, @RequestParam String id) {
//        franchiseProjectsService.alliancePass(employeePerformance, select, id);
//    }
    @PostMapping("alliancePass")
    public void AlliancePass(@RequestBody Map<String,String> map) {
        String select = map.get("select");
        String id = map.get("id");

        franchiseProjectsService.alliancePass(select, id);
    }

    /**
     * 发起杠杆计划   查看
     *
     * @return
     */
    @GetMapping("initiateLeveragePlans/view")
    public Map<String, Object> initiateLeveragePlansView(@RequestParam String messageId) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(messageId);
        //修改成已读
        //messageQueryService.setRead(messageId);
        //获取要操作的对象的详细消息的对象的id
        String messageLink = personalMessage.getMessageLink();
        LeveragePlanInformation leveragePlanInformation = leveragePlanService.initiateLeveragePlansView(messageLink);

        stringObjectHashMap.put("sender", personalMessage.getSenderName());//任务发起者
        stringObjectHashMap.put("messageType", personalMessage.getMessageType());//任务类型
        stringObjectHashMap.put("leveragePlan", leveragePlanInformation);
        stringObjectHashMap.put("customerName", leveragePlanInformation.getCustomerName());//客户姓名
        stringObjectHashMap.put("customerPhone", leveragePlanInformation.getCustomerPhone());//客户手机号
        stringObjectHashMap.put("customerId", leveragePlanInformation.getCustomerId());//客户id

        return stringObjectHashMap;
    }


    /**
     * 发起杠杆计划  通过
     */
    @PostMapping("initiateLeveragePlans/pass")
    public void initiateLeveragePlansPass(String messageId) {
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(messageId);
        //修改成已读
        messageQueryService.setRead(messageId);

        //获取要操作的对象的详细消息的对象的id
        String messageLink = personalMessage.getMessageLink();
        leveragePlanService.initiateLeveragePlansPass(messageLink);
    }

    /**
     * 发起杠杆计划  拒绝
     */
    @PostMapping("initiateLeveragePlans/refuse")
    public void initiateLeveragePlansRefuse(@RequestParam String messageId) {
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(messageId);
        //修改成已读
        messageQueryService.setRead(messageId);

        //获取要操作的对象的详细消息的对象的id
        String messageLink = personalMessage.getMessageLink();
        leveragePlanService.initiateLeveragePlansRefuse(messageLink);
    }

    /**
     * 获取通知详情
     * @param id
     * @return
     */
    @GetMapping("getNotice")
    public Map<String,String> getNotice(@RequestParam String id){
        Map<String, String> map = new HashMap<>();
        //查询个人中心消息
        //获取要操作的消息对象
        PersonalMessage personalMessage = messageQueryService.get(id);
        //修改成已读
        messageQueryService.setRead(id);
        //获取要操作的对象的详细消息的对象的id
        String messageLink = personalMessage.getMessageLink();
        Notice notice = messageViewService.getNotice(messageLink);
        map.put("senderName", personalMessage.getSenderName()); //发送人姓名
        map.put("senderId", personalMessage.getSenderId()); //发送人id
        map.put("receiverName", personalMessage.getReceiverName()); //接收人姓名
        map.put("receiverId", personalMessage.getReceiverId()); //接收人id
        map.put("messageType",personalMessage.getMessageType());//消息类型
        map.put("content", notice.getContent());//内容
        return map;
    }

}
